package com.oocl.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


public class DBUtil {
	public static DataSource ds;
	static{
		InitialContext cxt;
		try {
			cxt = new InitialContext();
			DataSource dataSource = (DataSource) cxt.lookup("java:comp/env/jdbc/TestDB");
			ds =  dataSource;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
public static Connection createConnection(){
	  Connection con = null;
		try {
			con = ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return con;
	} 

public static void close(Connection con,PreparedStatement stat,ResultSet rs){
	try {
		if (rs != null) {
			rs.close();

		}
		if (stat != null) {
			stat.close();
		}
		if (con != null) {
			con.close();
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}
	
	
}
